﻿using System.Web.Mvc;
using System.Web.Security;
using Viettel.VOFFICE.Web.Helpers;
using Viettel.VOFFICE.Web.Models;

namespace Viettel.VOFFICE.Web.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult LogOn()
        {
            //throw new Exception("Make a big issue");
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Default", "System");
            }
            return View();
        }

        /// <summary>
        /// Đăng nhập hệ thống
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    return RedirectToAction("Default", "System");

                }
                ModelState.AddModelError("", Resources.Resources.LogOnFail);
            }
            return View(model);
        }

        /// <summary>
        /// Thoát khỏi hệ thống
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn", "Home");
        }
    }
}
